package com.yxcz.controller;

import java.util.ArrayList;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yxcz.model.AddMenuForm;
import com.yxcz.model.JsonMenu;
import com.yxcz.model.Menu;
import com.yxcz.model.UpdateMenuForm;
import com.yxcz.service.MenuService;
import com.yxcz.service.RoleService;
import com.yxcz.service.UserService;

@Controller
@RequestMapping("/Menu")
public class MenuController {

	@Resource
	private RoleService roleService;
	@Resource
	private UserService userService;
	@Resource
	private MenuService menuService;

	/**
	 * 管理菜单页面
	 * 
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "index")
	public ModelAndView index(ModelAndView modelAndView) {

		// 传送菜单树JSON到前台
		ArrayList<JsonMenu> menus = menuService.list();
		ObjectMapper objectMapper = new ObjectMapper();
		String userMapJson;
		try {
			userMapJson = objectMapper.writeValueAsString(menus);
			modelAndView.addObject("menus", userMapJson);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		modelAndView.setViewName("Manager/Menu");
		return modelAndView;
	}

	/**
	 * AJAX获取菜单信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "getMenu")
	@ResponseBody
	public Menu getMenu(Integer id) {
		if (id == null)
			return null;
		return menuService.find(id);
	}

	/**
	 * 更新菜单信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "update")
	public ModelAndView update(@Validated UpdateMenuForm form, BindingResult bindingResult, ModelAndView modelAndView) {

		if (bindingResult.hasErrors()) {
			modelAndView.setViewName("redirect:/Common/500Page");
		} else {
			menuService.update(form);
			modelAndView.addObject("msg", "修改");
			modelAndView.setViewName("Common/success");
		}
		return modelAndView;

	}

	/**
	 * 删除菜单
	 * 
	 * @param id
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "delete")
	public ModelAndView delete(Integer id, ModelAndView modelAndView) {

		if (id == null) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}

		menuService.delete(id);
		modelAndView.addObject("msg", "删除");
		modelAndView.setViewName("Common/success");

		return modelAndView;

	}
	
	/**
	 * 增加菜单
	 * @param id
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "add")
	public ModelAndView add(@Validated AddMenuForm form, BindingResult bindingResult,  ModelAndView modelAndView) {
		if (bindingResult.hasErrors()) {
			modelAndView.setViewName("redirect:/Common/500Page");
		} else {
			menuService.add(form);
			
			modelAndView.addObject("msg", "增加");
			modelAndView.setViewName("Common/success");
		}

		return modelAndView;

	}


}
